package com.ruoyi.project.loan.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.loan.domain.RepaymentPlan;
import com.ruoyi.project.loan.service.IRepaymentPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 还款计划Controller
 *
 * @author ruoyi
 * @date 2025-11-11
 */
@RestController
@RequestMapping("/loan/plan" )
public class RepaymentPlanController extends BaseController {
    @Autowired
    private IRepaymentPlanService repaymentPlanService;

    /**
     * 查询还款计划列表(不分页)
     */
    @PreAuthorize("@ss.hasPermi('loan:plan:list')" )
    @GetMapping("/all/list" )
    public AjaxResult allList(RepaymentPlan repaymentPlan) {
        List<RepaymentPlan> list = repaymentPlanService.selectRepaymentPlanList(repaymentPlan);
        return success(list);
    }

    /**
     * 查询还款计划列表
     */
    @PreAuthorize("@ss.hasPermi('loan:plan:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(RepaymentPlan repaymentPlan) {
        startPage();
        List<RepaymentPlan> list = repaymentPlanService.selectRepaymentPlanList(repaymentPlan);
        return getDataTable(list);
    }

    /**
     * 导出还款计划列表
     */
    @PreAuthorize("@ss.hasPermi('loan:plan:export')" )
    @Log(title = "还款计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, RepaymentPlan repaymentPlan) {
        List<RepaymentPlan> list = repaymentPlanService.selectRepaymentPlanList(repaymentPlan);
        ExcelUtil<RepaymentPlan> util = new ExcelUtil<RepaymentPlan>(RepaymentPlan.class);
        util.exportExcel(response, list, "还款计划数据" );
    }

    /**
     * 获取还款计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('loan:plan:query')" )
    @GetMapping(value = "/{planId}" )
    public AjaxResult getInfo(@PathVariable("planId" ) Long planId) {
        return success(repaymentPlanService.selectRepaymentPlanByPlanId(planId));
    }

    /**
     * 新增还款计划
     */
    @PreAuthorize("@ss.hasPermi('loan:plan:add')" )
    @Log(title = "还款计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RepaymentPlan repaymentPlan) {
        return toAjax(repaymentPlanService.insertRepaymentPlan(repaymentPlan));
    }

    /**
     * 修改还款计划
     */
    @PreAuthorize("@ss.hasPermi('loan:plan:edit')" )
    @Log(title = "还款计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RepaymentPlan repaymentPlan) {
        return toAjax(repaymentPlanService.updateRepaymentPlan(repaymentPlan));
    }

    /**
     * 删除还款计划
     */
    @PreAuthorize("@ss.hasPermi('loan:plan:remove')" )
    @Log(title = "还款计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}" )
    public AjaxResult remove(@PathVariable Long[] planIds) {
        return toAjax(repaymentPlanService.deleteRepaymentPlanByPlanIds(planIds));
    }
}
